package com.technology.education.ext

import android.content.res.AssetManager
import androidx.fragment.app.Fragment
import com.technology.education.data.SpKeys
import com.tencent.mmkv.MMKV
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.lang.reflect.Type


///意见反馈
const val DEEP_LINK_FEED_BACK = "app://www.education.com/feedback"
const val DEEP_LINK_WITHDRAW_RESULT = "app://www.education.com/withdraw_result"

fun mmkv(): MMKV {
    return MMKV.defaultMMKV()
}

fun <T> saveData(key: String, data: T) {
    when (data) {
        is Int -> {
            mmkv().encode(key, data)
        }
        is String -> {
            mmkv().encode(key, data)
        }
        is Boolean -> {
            mmkv().encode(key, data)
        }
        is Double -> {
            mmkv().encode(key, data)
        }
        else -> {
            ""
        }
    }

}

/**
 * 将时长转换为 MM:ss格式
 * @param time 单位为秒
 */
fun formatTime(time: Int): String {
    return when {
        time < 60 -> {
            "00:${if (time > 9) time else "0$time"}"
        }
        time < 60 * 60 -> {
            val min = time / 60
            val second = time % 60
            "${if (min > 9) min else "0$min"}:${if (second > 9) second else "0$second"}"
        }
        else -> {
            val hour = time / 60 / 60
            val min = time / 60
            val second = time % 60
            "${if (hour > 9) hour else "0$hour"}:${if (min > 9) min else "0$min"}:${if (second > 9) second else "0$second"}"
        }
    }
}

/**
 * 将时长转换为 MM:ss格式
 * @param time 单位为xx min
 */
fun formatTimeMin(time: Int): String {
    return when {
        time < 60 -> {
            "${time}s"
        }
        time < 60 * 60 -> {
            val min = time / 60
            val second = time % 60
            "${min}min ${second}s"
        }
        else -> {
            val hour = time / 60 / 60
            val min = time % (60 * 60) / 60
            val second = time % 60
            "${hour}h ${min}min ${second}s"
        }
    }
}

fun saveSearchHistory(list: Set<String>) {
    mmkv().encode("searchHistory", list)
}

fun getSearchHistory(): Set<String> {
    return mmkv().getStringSet("searchHistory", setOf()) ?: setOf()
}

fun getSpString(key: String, default: String = ""): String {
    return mmkv().getString(key, default) ?: default
}

fun getSpBool(key: String, default: Boolean = false): Boolean {
    return mmkv().getBoolean(key, default)
}

fun getSpDouble(key: String, default: Double = 0.00): Double {
    return mmkv().decodeDouble(key, default)
}

fun isLogin(): Boolean {
    return getSpBool(SpKeys.IS_LOGIN, false)
}


fun Fragment.getJson(fileName: String): String {
    val stringBuilder = StringBuilder()
    try {
        val assetManager: AssetManager = requireContext().assets
        val bf = BufferedReader(
            InputStreamReader(
                assetManager.open(fileName)
            )
        )
        var line: String?
        while (bf.readLine().also { line = it } != null) {
            stringBuilder.append(line)
        }
    } catch (e: IOException) {
        e.printStackTrace()
    }
    return stringBuilder.toString()
}


